import Types from '../actionTypes';
import {getGoodsLibraryData, getInstructionCount} from '../../api';

const LEFT_NAV = () => {
  const getNav = localStorage.getItem('mynav');
  const tempNav = JSON.parse(getNav);
  const navArr = [];
  for (let k in tempNav) {
    navArr.push(tempNav[k]);
  }
  navArr.sort((a, b) => {
    return a.sort - b.sort;
  });
  console.log(tempNav);
  return {
    type: Types.LEFT_NAV,
    value: navArr
  };
};

const GET_INSTRUCTION_COUNT = username => {
  return new Promise((resolve, reject) => {
    getInstructionCount(username).then(res => {
      resolve({
        type: Types.GET_INSTRUCTION_COUNT,
        count: res.data
      });
    }).catch(err => {
      console.log(err);
    });
  });
};

const DATA_SOURCE = dataSource => {
  return {
    type: Types.DATA_SOURCE,
    dataSource
  };
};

export default {
  LEFT_NAV,
  GET_INSTRUCTION_COUNT,
  DATA_SOURCE
};
